package com.yunban.project.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.template.mapper.TemplateGroupStyleMapper;
import com.yunban.project.template.model.domain.TemplateGroupStyle;
import com.yunban.project.template.model.domain.TemplateStyle;
import com.yunban.project.template.service.TemplateGroupStyleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TemplateGroupStyleServiceImpl extends ServiceImpl<TemplateGroupStyleMapper, TemplateGroupStyle> implements TemplateGroupStyleService {
    @Override
    public List<Long> findGroupIdByStyleIds(List<Long> styleIds) {
        LambdaQueryWrapper<TemplateGroupStyle> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TemplateGroupStyle::getStyleId, styleIds);
        List<TemplateGroupStyle> templateGroupStyles = this.list(wrapper);
        List<Long> groupIds = templateGroupStyles.stream().map(TemplateGroupStyle::getGroupId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(groupIds)) {
            return List.of(-1L);
        }
        return groupIds;
    }

    @Override
    public void batchSave(long groupId, List<Long> styleIds) {
        List<TemplateGroupStyle> groupStyles = new ArrayList<>();
        for (Long styleId : styleIds) {
            TemplateGroupStyle templateGroupStyle = new TemplateGroupStyle();
            templateGroupStyle.setGroupId(groupId);
            templateGroupStyle.setStyleId(styleId);
            groupStyles.add(templateGroupStyle);
        }
        this.saveBatch(groupStyles);
    }

    @Override
    public void removeByGroupId(long groupId) {
        LambdaQueryWrapper<TemplateGroupStyle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateGroupStyle::getGroupId, groupId);
        this.remove(wrapper);
    }
}
